#pragma once
#include <string>
#include <fstream>
#include <deque>
#include <crtdbg.h>
namespace FIRSTDX
{ 
	std::string fileName[20] = {"Data1MB.data","Data2MB.data","Data3MB.data","Data4MB.data","Data5MB.data",
								"Data6MB.data","Data7MB.data","Data8MB.data","Data9MB.data","Data10MB.data",
								"Data11MB.data","Data12MB.data","Data13MB.data","Data14MB.data","Data15MB.data",
								"Data16MB.data","Data17MB.data","Data18MB.data","Data19MB.data","Data20MB.data"};
	CRITICAL_SECTION CriticalSection; 
	std::deque<int> toLoadList;
	int totalCount = 0;
	int nextNum = 0;
	int currentIdx = -1;
	void  checkBit(char* buff, int size);
	void addNum(int num);
	void loadToDo();
	void laoder_enter_point(void* );
	//////////////////////////////////////////////////////////////////////////
	void checkBit(char* buff, int size)
	{
		for (int i = 0; i< size; i++)
		{
			buff[i] = buff[i]^buff[i];
		}
	}

	void addNum(int num)
	{
		EnterCriticalSection(&CriticalSection); 

		for (int i = 0; i< num; ++i)
		{
			 
			nextNum += 1;
			if (nextNum >= 20)
			{
				nextNum-=20;
			}
			toLoadList.push_back(nextNum);
		}
		LeaveCriticalSection(&CriticalSection);
 
	}

	void loadToDo()
	{
		if(toLoadList.size() > 0)
		{
			char* buff;

			EnterCriticalSection(&CriticalSection); 
			currentIdx = toLoadList.front();
			toLoadList.pop_front();
			LeaveCriticalSection(&CriticalSection);
			_RPT1( _CRT_WARN, "current do  %d\n", currentIdx);

			std::ifstream ifs;
			ifs.open(fileName[currentIdx],std::ios::binary);
			int len = currentIdx*1024*1024;
			buff = new char[len];
			ifs.read(buff,len);
			ifs.close();
			checkBit(buff,len);
			delete[] buff;
			totalCount++;
			currentIdx = -1;
		}

	}




	void laoder_enter_point(void* )
	{
		while(true)
		{
			Sleep(2);
			loadToDo();
		}
	}
}